package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.BuildingDao;
import com.hqyj.dao.EmployeeDao;
import com.hqyj.pojo.Building;
import com.hqyj.pojo.Employee;
import com.hqyj.pojo.PageData;
import com.hqyj.service.BuildingService;
import com.hqyj.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {
    @Autowired
    private BuildingDao buildingDao;

    //获取全部楼层信息
    @Override
    public List<Building> getAllBuilding() {
        System.out.println("获取成功");
        List<Building> buildings = buildingDao.queryAll();
        return buildings;
    }

    //修改楼房信息
    @Override
    public int updateBuilding(Building building) {
        int result=buildingDao.updateOne(building);
        System.out.println("修改成功");
        return result;
    }

    //删除操作
    @Override
    public int deleteBuliding(Building building) {
        int reslut=buildingDao.deleteByBuilding(building);
        System.out.println("删除成功");
        return reslut;
    }

    @Override
    public PageData<Building> getBuildingByPage(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        //查询全部用户信息
        List<Building> buildings = buildingDao.queryAll();
        //创建pageInfo，pageInfo会计算分页相关信息
        PageInfo<Building> pageInfo = new PageInfo<>(buildings);
        //创建我们自己的数据模型
        PageData<Building> pageData = new PageData<>();

        pageData.setCurrentPage(pageNumber);
        pageData.setPageSize(pageSize);

        //填充总页数数据
        pageData.setTotalPage(pageInfo.getPages());

        //填充总记录数
        pageData.setTotalSize((int) pageInfo.getTotal());

        if (pageInfo.isHasNextPage()) {
            //有下一页，设置下一页的页码
            pageData.setNextPage(pageInfo.getNextPage());
        } else {
            //没有下一页，就是尾页
            pageData.setNextPage(pageInfo.getPages());
        }
        if (pageInfo.isHasPreviousPage()) {
            //有上一页，设置上一页的页码
            pageData.setPreviousPage(pageInfo.getPrePage());
        } else {
            //没有上一页，设置为首页
            pageData.setPreviousPage(1);
        }
        //设置每页的用户记录信息
        pageData.setList(pageInfo.getList());

        return pageData;
    }
}
